﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TimbreRecognition.FeatureExtraction
{
    public class FeatureWriter
    {
        private const string Delemiter = ";";

        public void Write(string file, List<Feature> features)
        {
            var groupedByNote = features.GroupBy(o => o.Note).OrderBy(o=>o.Key);

            StreamWriter writer = new StreamWriter(file);
            foreach (var noteGroup in groupedByNote)
            {
                writer.WriteLine(noteGroup.Key);

                var groupedByInstrument = noteGroup.GroupBy(o => o.Instrument).OrderBy(o=>o.Key);

                foreach (var instrumentGroup in groupedByInstrument)
                {
                    writer.WriteLine(instrumentGroup.Key);
                    foreach (var element in instrumentGroup)
                    {
                        double[] mfccs = element.MFCC;

                        foreach (double mfcc in mfccs)
                        {
                            writer.Write(mfcc.ToString("F2"));
                            writer.Write(Delemiter);                            
                        }
                        writer.Write(Environment.NewLine);
                    }
                }
            }

            writer.Close();
        }
    }
}
